package com.example.web.websystem.controller;


import com.example.web.websystem.ann.AnnPaging;
import com.example.web.websystem.controller.vo.CurrentUserVO;
import com.example.web.websystem.controller.vo.SysUserVO;
import com.example.web.websystem.controller.vo.TableDataVO;
import com.example.web.websystem.controller.vo.form.SysUserFormVO;
import com.example.web.websystem.dao.model.SysUserDO;
import com.example.web.websystem.response.BusinessError;
import com.example.web.websystem.response.CommonReturnData;
import com.example.web.websystem.response.GlobalCodeEnum;
import com.example.web.websystem.service.SysUserService;
import com.example.web.websystem.token.manage.TokenHandle;
import com.example.web.websystem.util.DozerUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * Created by YangGang on 2019/03/31 17:42.
 */
@RestController()
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    @ApiOperation("查询-当前登陆用户信息")
    @GetMapping("/getCurrentUser")
    public CommonReturnData getCurrentUser() {
        Long userId = TokenHandle.getCurrentTokenObject().getUserId();
        //获取用户基本信息
        SysUserDO sysUser = sysUserService.getSysUserByUserId(userId);
        if (sysUser.getUserId() == null) throw new BusinessError("用户不存在");
        //获取用户paths列表
        Set<String> pathsSet = sysUserService.listSysUserPaths(
                sysUser.getUserId(), sysUser.getGroupId(), sysUser.getIsAdministrator());
        if (pathsSet.size() == 0) throw new BusinessError("用户无权限");

        //返回
        CurrentUserVO vo = new CurrentUserVO();
        vo.setNickname(sysUser.getNickname());
        vo.setGroupId(sysUser.getGroupId());
        vo.setPaths(pathsSet);
        return CommonReturnData.success(vo);
    }

    @ApiOperation("查询-有效的用户列表")
    @GetMapping("/listNormalUsers")
    public CommonReturnData listNormalUsers() {
        List<SysUserDO> sysUserList = sysUserService.listNormalSysUsers();
        List<SysUserVO> voList = DozerUtil.mapList(sysUserList, SysUserVO.class);
        return CommonReturnData.success(voList);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", required = true),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页条数", required = true)
    })
    @ApiOperation("查询-全部的用户列表（分页）")
    @GetMapping("/listUsers")
    public CommonReturnData listUsers(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "10") int pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<SysUserDO> sysUserList = sysUserService.listAllSysUsers();
        List<SysUserVO> voList = DozerUtil.mapList(sysUserList, SysUserVO.class);
        TableDataVO vo=new TableDataVO();
        vo.setTotal(page.getTotal());
        vo.setList(voList);
        return CommonReturnData.success(vo);
    }

    @ApiOperation("查询-组织的用户列表")
    @GetMapping("/listGroupUsers")
    public CommonReturnData listGroupUsers(Long groupId) {
        List<SysUserDO> sysUserList = sysUserService.listSysUsersByGroupId(groupId);
        List<SysUserVO> voList = DozerUtil.mapList(sysUserList, SysUserVO.class);
        return CommonReturnData.success(voList);
    }


    @GetMapping("/getUser")
    public CommonReturnData getUser(long userId) {
        SysUserDO sysUser = sysUserService.getSysUserByUserId(userId);
        SysUserVO vo = DozerUtil.map(sysUser, SysUserVO.class);
        return CommonReturnData.success(vo);
    }

    @PostMapping("/addUser")
    public CommonReturnData addUser(SysUserFormVO sysUserFormVO) {
        Long userId = TokenHandle.getCurrentTokenObject().getUserId();
        SysUserDO sysUser = DozerUtil.map(sysUserFormVO, SysUserDO.class);
        sysUser.setCreateByUserId(userId);
        sysUserService.addSysUser(sysUser);
        return CommonReturnData.success(sysUser.getUserId());
    }

    @PostMapping("/updateUser")
    public CommonReturnData updateUser(SysUserFormVO sysUserFormVO) {
        if (sysUserFormVO.getUserId() == null) throw new BusinessError(GlobalCodeEnum.NOT_FIND_USER);
        SysUserDO sysUser = DozerUtil.map(sysUserFormVO, SysUserDO.class);
        int up = sysUserService.updateSysUser(sysUser);
        return CommonReturnData.success(up);
    }

    @PostMapping("/deleteUser")
    public CommonReturnData deleteUser(long userId) {
        int count = sysUserService.deleteSysUser(userId);
        return CommonReturnData.success(count);
    }

}
